package com.jf.cloud.user.controller.app;

import com.jf.cloud.api.auth.feign.AuthSocialFeignClient;
import com.jf.cloud.user.model.UserFans;
import com.jf.cloud.user.service.UserFansService;
import com.jf.cloud.user.service.UserService;
import com.jf.cloud.user.vo.UserFansVO;
import com.jf.cloud.user.dto.UserFansDTO;

import com.jf.cloud.api.auth.bo.UserInfoInTokenBO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

/**
 * 
 *
 * @author zz
 * @date 2023-12-01 14:30:05
 */
@RestController("appUserFansController")
@RequestMapping("/user_fans")
@Tag(name = "用户粉丝")
public class UserFansController {

    @Autowired
    private UserFansService userFansService;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private UserService userService;

    @GetMapping("/page")
    @Operation(summary = "获取列表", description = "分页获取列表")
    public ServerResponseEntity<PageVO<UserFans>> page(@Valid PageDTO pageDTO) {
        UserFans userFans=new UserFans();
        userFans.setUserId(AuthUserContext.get().getUserId());
        PageVO<UserFans> userFansPage = userFansService.page(pageDTO,userFans);
        for (int i = 0; i < userFansPage.getList().size(); i++) {
            UserFans userFans1=userFansPage.getList().get(i);
            userFans1.setNickName(userService.getByUserId(userFans1.getFansId()).getNickName());
            userFans1.setImgUrl(userService.getByUserId(userFans1.getFansId()).getPic());
        }

        return ServerResponseEntity.success(userFansPage);
    }

    @GetMapping("/mypage")
    @Operation(summary = "获取列表", description = "分页获取列表")
    public ServerResponseEntity<PageVO<UserFans>> mypage(@Valid PageDTO pageDTO) {
        UserFans userFans=new UserFans();
        userFans.setFansId(AuthUserContext.get().getUserId());
        PageVO<UserFans> userFansPage = userFansService.page(pageDTO,userFans);
        for (int i = 0; i < userFansPage.getList().size(); i++) {
            UserFans userFans1=userFansPage.getList().get(i);
            userFans1.setNickName(userService.getByUserId(userFans1.getUserId()).getNickName());
            userFans1.setImgUrl(userService.getByUserId(userFans1.getUserId()).getPic());
        }
        return ServerResponseEntity.success(userFansPage);
    }

    @GetMapping
    @Operation(summary = "获取", description = "根据id获取")
    public ServerResponseEntity<UserFans> getById(@RequestParam Long id) {
        return ServerResponseEntity.success(userFansService.getById(id));
    }

    @GetMapping("/find")
    @Operation(summary = "获取", description = "根据id获取")
    public ServerResponseEntity<UserFans> findById(@RequestParam Long userId) {
        Long fansId=AuthUserContext.get().getUserId();
        return ServerResponseEntity.success(userFansService.getByUserIdAndFansId(userId,fansId));
    }

    @GetMapping("/count")
    @Operation(summary = "获取", description = "粉丝数量")
    public ServerResponseEntity<Integer> count(@RequestParam Long userId) {
        if(userId!=null && userId!=0){
            return ServerResponseEntity.success(userFansService.count(userId));
        }else{
            return ServerResponseEntity.success(userFansService.count(AuthUserContext.get().getUserId()));

        }
    }

    @GetMapping("/mycount")
    @Operation(summary = "获取", description = "我关注数量")
    public ServerResponseEntity<Integer> mycount(@RequestParam Long userId) {
        if(userId!=null && userId!=0){
            return ServerResponseEntity.success(userFansService.mycount(userId));
        }else{
            return ServerResponseEntity.success(userFansService.mycount(AuthUserContext.get().getUserId()));

        }
    }

    @PostMapping
    @Operation(summary = "保存", description = "保存")
    public ServerResponseEntity<Boolean> save(@Valid @RequestBody UserFansDTO userFansDTO) {
        userFansDTO.setFansId(AuthUserContext.get().getUserId());
        boolean flag=true;
        if(userFansDTO.getStatus()==2){
            UserFans userFans = userFansService.getByUserIdAndFansId(userFansDTO.getUserId(),userFansDTO.getFansId());
            userFans.setStatus(userFansDTO.getStatus());
            userFansService.deleteById(userFans.getId());
            flag=false;
        }else{
            UserFans userFans = mapperFacade.map(userFansDTO, UserFans.class);
            userFansService.save(userFans);
        }

        return ServerResponseEntity.success(flag);
    }

    @PutMapping
    @Operation(summary = "更新", description = "更新")
    public ServerResponseEntity<Void> update(@Valid @RequestBody UserFansDTO userFansDTO) {
        UserFans userFans = mapperFacade.map(userFansDTO, UserFans.class);
        userFansService.update(userFans);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除", description = "根据id删除")
    public ServerResponseEntity<Void> delete(@RequestParam Long id) {
        userFansService.deleteById(id);
        return ServerResponseEntity.success();
    }
}
